package com.mc.moring.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

public class StatusBarUtils {
	
	private Activity mActivity;
	private int mColor = -1;
	private Drawable mDrawable;
	private boolean mIsDrawerLayout;
	private boolean mIsActionBar;
	private int mContentResourseIdInDrawer;
	
	public StatusBarUtils(Activity activity) {
		mActivity = activity;
	}
	
	public static StatusBarUtils with(Activity activity) {
		return new StatusBarUtils(activity);
	}
	
	public int getColor() {
		return mColor;
	}
	
	public StatusBarUtils setColor(int color) {
		mColor = color;
		return this;
	}
	
	public Drawable getDrawable() {
		return mDrawable;
	}
	
	public StatusBarUtils setDrawable(Drawable drawable) {
		mDrawable = drawable;
		return this;
	}
	
	public boolean isDrawerLayout() {
		return mIsDrawerLayout;
	}
	
	public boolean isActionBar() {
		return mIsActionBar;
	}
	
	public StatusBarUtils setIsActionBar(boolean actionBar) {
		mIsActionBar = actionBar;
		return this;
	}

	public StatusBarUtils setDrawerLayoutContentId(boolean drawerLayout, int contentId) {
		mIsDrawerLayout = drawerLayout;
		mContentResourseIdInDrawer = contentId;
		return this;
	}
	
	public void init() {
		fullScreen(mActivity);
		if (mColor != -1) {

			addStatusViewWithColor(mActivity, mColor);
		}
		if (mDrawable != null) {

			addStatusViewWithDrawble(mActivity, mDrawable);
		}
		if (isDrawerLayout()) {

			fitsSystemWindows(mActivity);
		}
		if (isActionBar()) {

			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
				rootView.setPadding(0, getStatusBarHeight(mActivity) + getActionBarHeight(mActivity), 0, 0);
			}
		}
	}
	

	public StatusBarUtils clearActionBarShadow() {
		if (Build.VERSION.SDK_INT >= 21) {
			ActionBar supportActionBar = ((AppCompatActivity) mActivity).getSupportActionBar();
			if (supportActionBar != null) {
				supportActionBar.setElevation(0);
			}
		}
		return this;
	}
	

	private void fitsSystemWindows(Activity activity) {
		ViewGroup contentFrameLayout = (ViewGroup) activity.findViewById(android.R.id.content);
		View parentView = contentFrameLayout.getChildAt(0);
		if (parentView != null && Build.VERSION.SDK_INT >= 14) {
			parentView.setFitsSystemWindows(true);

			if (parentView instanceof DrawerLayout) {
				DrawerLayout drawer = (DrawerLayout) parentView;
				drawer.setClipToPadding(false);
			}
		}
	}
	

	public static int getStatusBarHeight(Activity activity) {
		int result = 0;
		//获取状态栏高度的资源id
		int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			result = activity.getResources().getDimensionPixelSize(resourceId);
		}
		Log.e("getStatusBarHeight", result + "");
		return result;
	}
	

	public static int getActionBarHeight(Context context) {
		int result = 0;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			TypedValue tv = new TypedValue();
			context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
			result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
		}
		return result;
	}
	

	private void addStatusViewWithColor(Activity activity, int color) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			if (isDrawerLayout()) {

				ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);

				View parentView = rootView.getChildAt(0);
				LinearLayout linearLayout = new LinearLayout(activity);
				linearLayout.setOrientation(LinearLayout.VERTICAL);
				View statusBarView = new View(activity);
				ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
				statusBarView.setBackgroundColor(color);

				linearLayout.addView(statusBarView, lp);

				DrawerLayout drawer = (DrawerLayout) parentView;

				View content = activity.findViewById(mContentResourseIdInDrawer);

				drawer.removeView(content);

				linearLayout.addView(content, content.getLayoutParams());

				drawer.addView(linearLayout, 0);
			} else {

				ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
				rootView.setPadding(0, getStatusBarHeight(mActivity), 0, 0);
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					activity.getWindow().setStatusBarColor(color);
				} else {
					ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
					View statusBarView = new View(activity);
					ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
					statusBarView.setBackgroundColor(color);
					decorView.addView(statusBarView, lp);
				}
			}
		}
	}
	

	private void addStatusViewWithDrawble(Activity activity, Drawable drawable) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			//占位状态栏
			View statusBarView = new View(activity);
			ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				statusBarView.setBackground(drawable);
			} else {
				statusBarView.setBackgroundDrawable(drawable);
			}
			if (isDrawerLayout()) {

				ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);

				View parentView = rootView.getChildAt(0);
				LinearLayout linearLayout = new LinearLayout(activity);
				linearLayout.setOrientation(LinearLayout.VERTICAL);

				linearLayout.addView(statusBarView, lp);

				DrawerLayout drawer = (DrawerLayout) parentView;

				View content = activity.findViewById(mContentResourseIdInDrawer);

				drawer.removeView(content);

				linearLayout.addView(content, content.getLayoutParams());

				drawer.addView(linearLayout, 0);
			} else {

				ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
				decorView.addView(statusBarView, lp);

				ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
				rootView.setPadding(0, getStatusBarHeight(mActivity), 0, 0);
			}
		}
	}
	

	private void fullScreen(Activity activity) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

				Window window = activity.getWindow();
				View decorView = window.getDecorView();

				int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
				window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
				decorView.setSystemUiVisibility(option);
				window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
				window.setStatusBarColor(Color.TRANSPARENT);

//                window.setNavigationBarColor(Color.TRANSPARENT);
			} else {
				Window window = activity.getWindow();
				WindowManager.LayoutParams attributes = window.getAttributes();
				int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
				int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
				attributes.flags |= flagTranslucentStatus;
//                attributes.flags |= flagTranslucentNavigation;
				window.setAttributes(attributes);
			}
		}
	}
	

	public static void setStatusTransparent(Activity activity) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
				Window window = activity.getWindow();
				
				WindowManager.LayoutParams attributes = window.getAttributes();
				int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
				int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
//                attributes.flags |= flagTranslucentStatus;
				attributes.flags |= flagTranslucentNavigation;
				window.setAttributes(attributes);
				
				window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
				window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
				window.setStatusBarColor(Color.TRANSPARENT);
				window.setNavigationBarColor(Color.TRANSPARENT);
			} else {
				Window window = activity.getWindow();
				WindowManager.LayoutParams attributes = window.getAttributes();
				int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
				int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
				attributes.flags |= flagTranslucentStatus;
				attributes.flags |= flagTranslucentNavigation;
				window.setAttributes(attributes);
			}
		}
	}
}
